package aries.utils;

/******************************************************************************
 * You may modify, publish, and use Aries for your own purpose. BUT
 * DON'T REMOVE THIS TEXT.
 * Visit my facebook: http://www.facebook.com/freeskies or
 * Follow my twitter: @freeskys
 *
 *
 * Author: Harditya Rahmat Ramadhan
 * Licence: GPLv2
 *****************************************************************************/

import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.GrayFilter;
import javax.swing.Icon;
import javax.swing.ImageIcon;

public class ImageUtil extends Util {
    
    /**
     * Capture the whole screen.
     * 
     * @param filename
     * @param imageFormat
     * @throws AWTException
     * @throws IOException 
     */
    public void takeScreenshot(String filename) throws AWTException, IOException {
        takeScreenshot(filename, new Point(0, 0), Toolkit.getDefaultToolkit().getScreenSize());
    }
    
    /**
     * Capture a screenshot from a specified point and dimension.
     * 
     * @param filename
     * @param imageFormat
     * @param point
     * @param dimension
     * @throws AWTException
     * @throws IOException 
     */
    public void takeScreenshot(String filename, Point point, Dimension dimension) throws AWTException, IOException {
        BufferedImage image = new Robot().createScreenCapture(new Rectangle(point, dimension));
        ImageIO.write(image, TextUtil.findExtension(filename), new File(filename));
    }
    
    /**
     * Grascaling your image.
     * 
     * @param icon
     * @return Icon
     */
    public Icon grayscale(Icon icon) {
        if (icon instanceof ImageIcon) {
            java.awt.Image grayImage = GrayFilter.createDisabledImage(((ImageIcon)icon).getImage());
            return new ImageIcon(grayImage);
        }
        throw new IllegalArgumentException("Can't grayscaling your image");
    }
    
    /**
     * Create image icon.
     * 
     * @param path
     * @return ImageIcon
     */
    public static ImageIcon createImageIcon(String path) {
        ImageUtil iutil = new ImageUtil();
        return iutil.imageIcon(path);
    }
    
    /**
     * Create image.
     * 
     * @param path
     * @return Image
     */
    public static Image createImage(String path) {
        ImageUtil iutil = new ImageUtil();
        return iutil.image(path);
    }
    
    /**
     * Create image icon.
     * 
     * @param path
     * @return ImageIcon
     */
    private ImageIcon imageIcon(String path) {
        URL url = getClass().getResource(path);
        if (url != null) {
            return new ImageIcon(url);
        } else {
            return null;
        }
    }
    
    /**
     * Get image.
     * 
     * @param path
     * @return Image
     */
    private Image image(String path) {
        return createImageIcon(path).getImage();
    }
    
}
